package com.example.pro5_webflux_r2dbc_book.controllers;

import com.example.pro5_webflux_r2dbc_book.model.Book;
import com.example.pro5_webflux_r2dbc_book.service.BookService;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
//
@RestController
@RequestMapping("/books")
public class BookController {
    private final BookService bookService;

    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @PostMapping("/add")
    public Mono<Book> addBook(@RequestBody Mono<Book> bookMono) {
        return bookMono.flatMap(bookService::addBook);
    }

    @GetMapping("/all")
    public Flux<Book> getAllBooks() {
        return bookService.getAllBooks();
    }

    @GetMapping("/{id}")
    public Mono<Book> getBookById(@PathVariable Long id) {
        return bookService.getBookById(id);
    }

    @PutMapping("/{id}")
    public Mono<Book> updateBook(@PathVariable Long id, @RequestBody Mono<Book> bookMono) {
        return bookMono.flatMap(book -> bookService.updateBook(id, book));
    }

    @DeleteMapping("/{id}")
    public Mono<Void> deleteBook(@PathVariable Long id) {
        return bookService.deleteBook(id);
    }
}
